//
//  KTBaseNavVC.m
//  KTLotteryTicket
//
//  Created by CoolKi on 16/3/4.
//  Copyright © 2016年 CoolKi. All rights reserved.
//

#import "KTBaseNavVC.h"

@interface KTBaseNavVC ()<UIGestureRecognizerDelegate,UINavigationControllerDelegate>

@property (nonatomic,strong) id popDelegate;

@end

@implementation KTBaseNavVC

//load方法，当类被加载到内存中就会被调用，只调用一次
//+(void)load{}

//当 当前类 或者他的子类第一次使用的时候 调用
+ (void)initialize
{
    //获取整个应用程序下所有的导航条.
    //UINavigationBar *bar = [UINavigationBar appearance];
    
    //获取指定类下面的导航条
    UINavigationBar * navBar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[KTBaseNavVC class]]];
    
    //设置背景(设置背影图片时,必须得要使用)
    [navBar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];//default 默认是从64开始的
    
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    dic[NSForegroundColorAttributeName] = [UIColor whiteColor];
    [navBar setTitleTextAttributes:dic];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.popDelegate = self.interactivePopGestureRecognizer.delegate;
    self.delegate = self;
    UIPanGestureRecognizer * panGs = [[UIPanGestureRecognizer alloc]initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
    panGs.delegate = self;
    [self.view addGestureRecognizer:panGs];
}


#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    if (self.childViewControllers.count == 1) {
        self.interactivePopGestureRecognizer.delegate = self.popDelegate;
    }
}

#pragma mark - 重写系统push方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    if (self.childViewControllers.count != 0) {
        //给当前 viewController设置
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageWithOriginImage:@"NavBack"] style:0 target:self action:@selector(popViewController)];
        
        self.interactivePopGestureRecognizer.delegate = nil;
    }
    
    //系统默认 跳转界面做法
    [super pushViewController:viewController animated:animated];
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    return self.childViewControllers.count != 1;
}

- (void)popViewController{
    [self popViewControllerAnimated:YES];
}








@end
